<#import "../common/layout.ftl" as layout/>
<#import "../common/dataTables.ftl" as dataTables/>
<#import "../common/modern-forms.ftl" as mform/>
<#import "../common/modal.ftl" as modal/>
<!DOCTYPE html>
<html>
<@layout.head title="内部管理">
    <@dataTables.head/>
    <@dataTables.buttons_head/>
    <@dataTables.select_head/>
    <@dataTables.editor_head/>
    <@dataTables.fixedHeader_head/>
    <@mform.head/>
    <@select.head/>
</@layout.head>
<body>
<@layout.page>
    <div class="main-box">
        <div class="content">
            <!-- 表格 -->
            <div style="position: relative; ">
                <div style="width:100%;display: flex;">
                    <div style="flex: 0.65;position: relative">
                        <div style="position: absolute; top:80px;left:0px;width: 100% ">

                            <@dataTables.commonTable id="mainTable1"/>
                        </div>
                        <div id="searchGroup" class="btn-group" style="position: absolute; ">
                            <div style='width:250px;float:left;margin:5px'>
                                <select class='form-control' id='oneOrganization'>
                                    <#list organizationName as item>
                                        <option value="${item.id?c}">${item.name}</option>
                                    </#list>
                                </select>
                            </div>

                        </div>
                        <div id="btnGroup" class="btn-group" style="position: absolute; top:40px;">
                            <#--<button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"-->
                                    <#--onclick="javascript:addMain();">-->
                                <#--<i class="fa fa-plus"></i> 添加一级机构-->
                            <#--</button>-->

                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:addSon();">
                                <i class="fa fa-edit"></i> 添加子机构
                            </button>
                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:editdetailOrganization();">
                                <i class="fa fa-edit"></i> 修改
                            </button>

                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:deldetailOrganization();">
                                <i class="fa fa-minus"></i> 删除
                            </button>
                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:refreshOrganization();">
                                <i class="fa fa-search"></i> 查询机构
                            </button>
                        </div>

                    </div>
                    <div style="width: 10px;"></div>
                    <div style="flex: 1;position: relative">
                        <div style="position: absolute; top:45px;left:0px;">
                            <@dataTables.commonTable id="mainTable2"/>
                        </div>
                        <div id="searchGroup" class="btn-group" style="position: absolute; ">
                            <input id="staffInformation" name="staffInformation" placeholder='姓名/手机号'
                                   style="margin-left:135px; display: none"/>

                        </div>
                        <div id="btnGroup" class="btn-group" style="position: absolute; top:40px;">
                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    onclick="javascript:refreshStaff();">
                                <i class="fa fa-search"></i> 查询人员
                            </button>
                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:newdetailStaff();">
                                <i class="fa fa-plus"></i> 添加人员
                            </button>

                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:editdetailStaff();">
                                <i class="fa fa-edit"></i> 修改人员
                            </button>

                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:deldetailStaff();">
                                <i class="fa fa-minus"></i> 删除人员
                            </button>
                            <button id="btnAddTopMenu" type="button" class="btn btn-blue btn-sm"
                                    style="margin-left: 5px;"
                                    onclick="javascript:yzm();">
                                <i class="fa fa-refresh"></i> 重置验证码
                            </button>
                        </div>

                    </div>
                </div>

            </div>

        </div>
    </div>
    <@modal.editModal id="modal-detail" buttonId="btndetailSave" width="450px" height="100px">
        <@mform.container width="450" id="editForm">
            <input id="id" name="id" type="hidden"/>
            <@mform.row>
                <@mform.input_text id='name' name='name' size=6 label='机构名称' placeholder='机构名称' icon='fa-edit' />
                <@mform.input_text id='code' name='code' size=6 label='机构编号' placeholder='机构编号' icon='fa-edit' />
            </@mform.row>
            <@mform.row>
                <@mform.select2 id='organizationType' name='organizationType' label="机构类型"  size=6>
                    <#list organizationType as item>
                        <option value="${item.code}">${item.name}</option>
                    </#list>
                </@mform.select2>
            </@mform.row>
            <@mform.row>
                <@mform.input_text id='superOrganizationId' name='superOrganizationId' size=12 label='上级机构id' placeholder='上级机构id' icon='fa-edit' />
            </@mform.row>
        </@mform.container>
    </@modal.editModal>
    <@modal.editModal id="modal-detail1" buttonId="btndetailSave1" width="450px" height="100px" >
        <@mform.container width="450" id="editForm1">
            <input id="id" name="id" type="hidden"/>
            <input id="organizationId" name="organizationId" type="hidden"/>
            <@mform.row>
                <@mform.input_text id='name' name='name' size=6 label='人员姓名' placeholder='人员姓名' icon='fa-edit' />
                <@mform.input_text id='telephone' name='telephone' size=6 label='手机号' placeholder='手机号' icon='fa-edit' />
            </@mform.row>
            <@mform.row>
                <@mform.input_text id='job' name='job' size=6 label='职务' placeholder='职务' icon='fa-edit' />
                <@mform.select2 id='role' name='role' label="角色"  size=6>
                    <option value="5" selected="selected">默认普通用户</option>
                    <#list staffRole as item>
                        <option value="${item.cRoleID}">${item.cRoleName}</option>
                    </#list>
                </@mform.select2>
            <#--
             <@mform.input_text id='role' name='role' size=6 label='角色' placeholder='角色' icon='fa-edit' />-->
            </@mform.row>
            <@mform.row>
               <#-- <@mform.input_text id='organizationId' name='organizationId' size=6 label='所属机构id' placeholder='所属机构id' icon='fa-edit' />-->

            </@mform.row>
        </@mform.container>
    </@modal.editModal>
</@layout.page>
<@layout.foot>
    <@dataTables.foot/>
    <@dataTables.buttons_foot/>
    <@dataTables.select_foot/>
    <@dataTables.fixedHeader_foot/>
    <@dataTables.editor_foot/>
    <@mform.foot/>
    <script src="${request.contextPath}/assets/js/jquery.serializejson.min.js?v=3653060703"></script>
    <script type="text/javascript">
        var validState = {
            <#list validState as item>
            "${item.code}": "${item.name}",
            </#list>
        }
        var columns1 = [
            {'sWidth': '100px', 'title': 'id', 'data': 'id'},
            {'sWidth': '100px', 'title': '机构名称', 'data': 'name'},
            {'sWidth': '100px', 'title': '机构编码', 'data': 'code'}
        ]
        var mainTable1 = $('#mainTable1').winningTable({
            columns: columns1,
            pageLength: 5,
            fixedHeader: false,
            searching: false,
            ordering: false, dom: 'Bfrtip',
            "columnDefs": [{
                "targets": [0],
                "visible": false
            }],
            buttons: []
        });
        $("#mainTable1").css("cssText","margin-top:0px !important");
        var columns2 = [
            {'sWidth': '100px', 'title': 'ID', 'data': 'id'},
            {'sWidth': '150px', 'title': '人员姓名', 'data': 'name'},
            {'sWidth': '200px', 'title': '手机号', 'data': 'telephone'},
            {'sWidth': '150px', 'title': '职务', 'data': 'job'},
            {'sWidth': '200px', 'title': '角色', 'data': 'cRoleName'},
            {'sWidth': '100px', 'title': '验证码', 'data': 'validCode'},
            {'sWidth': '250px', 'title': '创建时间', 'data': 'createTime'}
        ]
        var mainTable2 = $('#mainTable2').winningTable({
            columns: columns2,
            pageLength: 5,
            fixedHeader: false,
            searching: true,
            ordering: false, dom: 'Bfrtip',
            "columnDefs": [{
                "targets": [0],
                "visible": false
            }],
            buttons: [],
            "rowCallback": function (row, data, index) {
            }
        });
        // 机构查询
        function refreshOrganization() {
            var data = {
                datas: JSON.stringify({
                    oneOrganization: $("#oneOrganization").val()
                })
            }
            mainTable1.queryData("${request.contextPath}/inner/organization/queryList", data);
        }
        refreshOrganization();

        // 人员查询
        function refreshStaff() {
            var oid=null;
            var datas = mainTable1.getSelectedDatas();
            if (datas.length == 1) {
                oid=datas[0].id;
            }else {
                oid=$("#oneOrganization").val();
            }

            var data = {
                datas: JSON.stringify({
                    organizationId:oid,
                    staffInformation: $("#staffInformation").val()
                })
            }
            mainTable2.queryData("${request.contextPath}/inner/staff/queryList",data);
        }
        refreshStaff();
        //新建一级机构
        function addMain() {
            $('#modal-detail').find(".modal-title").text('添加一级机构');
            clearForm($('#modal-detail'));
            $("#btndetailSave").unbind();
            $("#btndetailSave").click(saveMainOrganization);
            $('#modal-detail').modal();
            $('#modal-detail').find("#superOrganizationId").parent().attr("style", "display:none");
        }
        // 添加子机构
        function addSon() {
            if ($('#oneOrganization').val() == 0) {
                swal("错误", "请选择下拉框中的机构", "error");
                return;
            }
            $('#modal-detail').find(".modal-title").text('添加子机构');
            clearForm($('#modal-detail'));
            $("#btndetailSave").unbind();
            var superOrganizationId=$("#oneOrganization").val();
            $('#modal-detail').find("#organizationId").val(superOrganizationId);
            $("#btndetailSave").click(saveOrganization);
            $('#modal-detail').modal();
            $('#modal-detail').find("#superOrganizationId").parent().attr("style", "display:none");
            $('#modal-detail').find("#organizationType").parent().attr("style", "display:none");
        }
        function editdetailOrganization() {
            var datas = mainTable1.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var $modal = $("#modal-detail");
            $modal.find(".modal-title").text("修改机构信息");
            clearForm($modal);
            var data = datas[0];
            initFormItemData($("#editForm"), data);
            $modal.modal();
            $("#btndetailSave").unbind();
            $("#btndetailSave").click(editOrganization);
            $('#modal-detail').find("#superOrganizationId").parent().attr("style", "display:none");
        }
        //添加人员
        function newdetailStaff() {
            var datas = mainTable1.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择子机构", "error");
                return;
            }
            $('#modal-detail1').find(".modal-title").text('新增人员');
            clearForm($('#modal-detail1'));
            $("#btndetailSave1").unbind();
            var data = datas[0];
            $("#organizationId").val(data.id);
            $("#btndetailSave1").click(saveStaff);
            $('#modal-detail1').find("#organizationId").val(data.id);
            $('#modal-detail1').modal();
        }
        //修改人员信息
        function editdetailStaff() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var $modal = $("#modal-detail1");
            $modal.find(".modal-title").text("修改人员信息");
            clearForm($modal);
            var data = datas[0];
            initFormItemData($("#editForm1"), data);
            $modal.modal();
            $("#btndetailSave1").unbind();
            $("#btndetailSave1").click(saveStaff);
        }
        //验证码
        function yzm() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data1 = datas[0];
            var data = {
                datas: JSON.stringify({
                    id: data1.id
                })
            }
            swal({
                title: "确定重置此验证码",
                text: "是否确定修改此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (result.value)
                    mainTable2.queryData("${request.contextPath}/cust/staff/editYZM", data);
                refreshStaff();
            })
        }

        function saveOrganization() {
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#modal-detail form").serializeJSON();
                var superOrganizationId=$("#oneOrganization").val();
                requestData.superOrganizationId=superOrganizationId;
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/inner/organization/save',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail").modal('hide');
                                refreshOrganization();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function saveMainOrganization() {
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#modal-detail form").serializeJSON();
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/inner/organization/save',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail").modal('hide');
                                refreshOrganization();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function editOrganization() {
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#modal-detail form").serializeJSON();
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/inner/organization/save',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail").modal('hide');
                                refreshOrganization();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function saveStaff() {
            swal({
                title: "确定保存",
                text: "是否确定保存此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = $("#modal-detail1 form").serializeJSON();
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/inner/staff/save',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail1").modal('hide');
                                refreshStaff();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        function password() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            var params = {
                datas: JSON.stringify({
                    id: data.id.toString()
                })
            };
            swal({
                title: "确定初始化密码",
                text: "是否确定初始化密码为123456",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/inner/staff/password',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                $("#modal-detail1").modal('hide');
                                refreshStaff();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        // 删除机构
        function deldetailOrganization() {
            var datas = mainTable1.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/inner/organization/delete',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshOrganization();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
        // 删除人员
        function deldetailStaff() {
            var datas = mainTable2.getSelectedDatas();
            if (datas == null || datas.length == 0 || datas.length > 1) {
                swal("错误", "请选择一行数据", "error");
                return;
            }
            var data = datas[0];
            swal({
                title: "确定删除",
                text: "是否确定删除此数据",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "是的,确定",
                cancelButtonText: "取消"
            }).then(function (result) {
                if (!result.value)
                    return;
                var requestData = {
                    id: data.id
                };
                var params = {datas: JSON.stringify(requestData)};
                $.ajax({
                    type: 'POST',
                    data: params,
                    url: '${request.contextPath}/inner/staff/delete',
                    success: function (data) {
                        //成功之后
                        if (data.success) {
                            swal({
                                title: "成功",
                                text: "操作成功!",
                                type: "success",
                                showCancelButton: false,
                                confirmButtonText: "确定"
                            }).then(function (result) {
                                refreshStaff();
                            });
                        } else {
                            swal("错误", data.errorMsg, "error");
                        }
                    },
                    error: function (xhr, type) {
                        swal("错误", "系统发生内部错误!请稍后再试!", "error");
                    }
                });
            });
        }
    </script>
</@layout.foot>
</body>
</html>